package com.sandy.demo.controller;

import com.sandy.demo.jpa.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HelloController {
    @Autowired
    UserRepository userRepo;

    @Autowired
    MovieRepository movieRepo;

    @RequestMapping("hello/{name}")
    public String say(@PathVariable String name) {
        return "hello," + name;
    }

    @RequestMapping("save/{name}")
    public User helloUser(@PathVariable String name) {
        User u = new User();
        u.setUsername(name);
        u.setPassword("123456");
        userRepo.save(u);
        return u;
    }

    @RequestMapping("movie/{name}")
    public Movie saveMovie(@PathVariable String name) {
        Movie m = new Movie();
        m.setName(name);
        m.setType(MovieType.CARTOON);
        m.setType2(MovieType.CARTOON);
        m.setType3(MovieType.CARTOON);
//        m.setAddress(new AddressType("home","work"));
        return movieRepo.save(m);
    }

    @RequestMapping("allMovie")
    public List<Movie> allMovie() {
        return movieRepo.findAll();
    }
}
